﻿using BySoft.Server.Common;
using BySoft.Server.Common.Entity;
using BySoft.Server.DataAccess;
using BySoft.Server.DataAccess.UserCode;
using OneSoft.DataAccess;
using OneSoft.EntityCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace BySoft.Server.Business.UserCode
{
    public partial class BsOrderBusiness
    {

        public static bool IsOrderIDExists(string OrderID)
        {
            bool isTag = true;
            try
            {
                string str = @"select * from BS_Order where OrderID=@OrderID";
                DataAccessParameterCollection dpc = new DataAccessParameterCollection();
                dpc.AddWithValue("@OrderID", OrderID);
                DataTable dt = DataAccessCommon.Select(str, dpc).Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    isTag = true;
                }
                else
                {
                    isTag = false;
                }
            }
            catch (Exception ex)
            {
                isTag = false;
                throw ex;
            }
            return isTag;
        }

        public static bool SaveOrUpdate(BsOrderEntity entity, DataTable OrderDetailDT, string type)
        {
            bool istag = true;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                broker.BeginTransaction();
                try
                {
                    if (type == "Save")
                    {
                        DataAccessCommon.Insert(entity, broker);
                    }
                    else if (type == "Update")
                    {
                        DataAccessCommon.UpdateOnly(entity, broker);
                    }
                    if (OrderDetailDT != null && OrderDetailDT.Rows.Count > 0)
                    {
                        foreach (DataRow drItem in OrderDetailDT.Rows)
                        {
                            BsOrderdetailEntity DetailEntity = new BsOrderdetailEntity();
                            DetailEntity.OrderID = entity.OrderID;
                            DetailEntity.OrderNum = CommonMethod.ConvertToDecimal(drItem["OrderNum"]);
                            DetailEntity.ProductID = CommonMethod.GetString(drItem["ProductID"]);
                            DetailEntity.CustomerPartNo = CommonMethod.GetString(drItem["CustomerPartNo"]);
                            DetailEntity.Nums = CommonMethod.ConvertToDecimal(drItem["Nums"]);
                            DetailEntity.Price = CommonMethod.ConvertToDecimal(drItem["Price"]);
                            decimal amount = CommonMethod.ConvertToDecimal(drItem["Nums"]) * CommonMethod.ConvertToDecimal(drItem["Price"]);
                            DetailEntity.Amount = Math.Round(amount, 2);
                            if (entity.Currency.Equals("1"))//人民币
                            {
                                decimal? tax=entity.TaxRate / 100;
                                DetailEntity.TaxAmount = amount * (1 + tax);
                            }
                            else
                            {
                                DetailEntity.TaxAmount = amount;
                            }
                            DetailEntity.RMBPrice = DetailEntity.Price;
                            DetailEntity.WorkOrderNo = CommonMethod.GetString(drItem["WorkOrderNo"]);
                            DetailEntity.IsFinish = CommonMethod.GetString(drItem["IsFinish"]);
                            string strIsNew = CommonMethod.GetString(drItem["IsNew"]);
                            string strIsDel = CommonMethod.GetString(drItem["IsDel"]);
                            if (strIsNew == "1" && strIsDel != "1")
                            {
                                DataAccessCommon.Insert(DetailEntity, true, broker);
                            }
                            if (strIsNew != "1" && strIsDel != "1")
                            {
                                DetailEntity.OrderDetailID = CommonMethod.ConvertToDecimal(drItem["OrderDetailID"]);
                                DataAccessCommon.UpdateOnly(DetailEntity, broker);
                            }
                            if (strIsNew != "1" && strIsDel == "1")
                            {
                                //删除明细
                                ClsCommDA.Del("BS_OrderDetail", "OrderDetailID", CommonMethod.GetString(drItem["OrderDetailID"]), broker);
                            }
                        }
                    }
                    broker.Commit();
                }
                catch
                {
                    istag = false;
                    broker.Rollback();
                    throw;
                }
                return istag;
            }
        }

        public static bool Del(string strCode)
        {
            bool isResult = true;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    DataAccessCommon.Delete(strCode, "OrderID", "BS_OrderDetail");
                    DataAccessCommon.Delete(strCode, "OrderID", "BS_Order");
                    broker.Commit();
                }
                catch (Exception ex)
                {
                    isResult = false;
                    broker.Rollback();
                    throw ex;
                }
            }
            return isResult;
        }

        public static DataTable SelectList(string OrderID, string ProductID, string ProductName, string cpFrom, string cpEnd, int pageIndex, int PageSize, out int m_count)
        {
            return BsOrderDataAccess.SelectList(OrderID, ProductID, ProductName, cpFrom, cpEnd, pageIndex, PageSize, out m_count);
        }

        public static DataTable GetOrderData(string id)
        {
            return BsOrderDataAccess.GetOrderData(id);
        }
    }
}
